/*
 * Copyright 2008-2023 dexian.vip. All rights reserved.
 * Support: http://www.dexian.vip
 * License: http://www.dexian.vip/license
 */

package vip.dexian.admin.manager.impl;

import vip.dexian.admin.assembler.RoleDepartmentDTOAssembler;
import vip.dexian.admin.dao.RoleDepartmentDao;
import vip.dexian.admin.dto.RoleDepartmentDTO;
import vip.dexian.admin.entity.RoleDepartment;
import vip.dexian.admin.manager.RoleDepartmentManager;
import vip.dexian.common.query.Filter;
import vip.dexian.common.query.Listable;
import vip.dexian.common.utils.FilterUtils;
import vip.dexian.core.config.redis.RedisCacheKeyAttributes;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * 角色关联部门
 *
 * @author 挺好的 2023年06月09日 10:32
 */
@Service ("roleDepartmentManagerImpl")
public class RoleDepartmentManagerImpl implements RoleDepartmentManager {

    /**
     * 角色->部门Dao
     */
    @Resource (name = "roleDepartmentDaoImpl")
    private RoleDepartmentDao roleDepartmentDao;

    /**
     * 属性复制
     */
    @Resource (name = "roleDepartmentDTOAssemblerImpl")
    private RoleDepartmentDTOAssembler roleDepartmentDTOAssembler;

    /**
     * 根据角色id查找关联的部门信息。该方法会开启缓存
     *
     * @param roleIds
     *         角色id列表
     *
     * @return {@link vip.dexian.admin.dto.RoleDepartmentDTO}
     */
    @Override
    @Transactional (propagation = Propagation.SUPPORTS, readOnly = true)
    @Cacheable (value = RedisCacheKeyAttributes.ROLE_DEPARTMENT_KEY)
    public List <RoleDepartmentDTO> findListByRoleIds (List <Long> roleIds) {
        if (CollectionUtils.isEmpty(roleIds)) {
            return Collections.emptyList();
        }

        List <Filter> filters = FilterUtils.builder().in("roleId", roleIds).build();
        Listable listable = Listable.builder().filters(filters).build();

        List <RoleDepartment> departments = this.roleDepartmentDao.findList(listable);

        return this.roleDepartmentDTOAssembler.toDTO(departments);
    }

}
